package com.lanqiao.gxnushop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanqiao.gxnushop.entity.Sellers;
import com.lanqiao.gxnushop.service.SellersService;
import com.lanqiao.gxnushop.service.UserLoginsService;
import com.lanqiao.gxnushop.util.UUIdUtil;
import com.lanqiao.gxnushop.valueObject.Page;
import com.lanqiao.gxnushop.valueObject.SellerNeed;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.io.File;
import java.util.ArrayList;

@Log4j
@RestController
public class SellersController {
    @Autowired
    private SellersService sellersService;
    @Autowired
    private UserLoginsService userLoginsService;

    @Operation(summary = "增加卖家  传入的参数需要：name,sfz,email,phone,adress")
    @Parameter(name = "sellers",description = "卖家信息")
    @PostMapping("/sellersCtrl/add")
    public boolean add(Sellers sellers){
        sellers.setSellerId(UUIdUtil.getUserId());
        return sellersService.add(sellers);
    }

    @Operation(summary = "通过卖家Id删除卖家信息,同时删除卖家的登录信息")
    @Parameter(name = "sellerId",description = "卖家Id")
    @DeleteMapping("/sellerCtrl/delete")
    public boolean delete(String sellerId){
        //删除登录信息
        userLoginsService.delete(sellerId);
        //删除卖家信息
        return sellersService.delete(sellerId);
    }
    @Operation(summary = "更新卖家信息 name，sfz,email,phone,address ; 返回值boolean")
    @Parameter(name = "sellers",description = "卖家信息")
    @PutMapping("/sellersCtrl/update")
    public boolean update(Sellers sellers){
        return sellersService.update(sellers);
    }

    @Operation(summary = "注册成为卖家时   管理员审核通过后，将状态state置为1，表示注册卖家成功 返回值boolean")
    @Parameter(name = "sellerId",description = "卖家信息")
    @PutMapping("/sellerCtrl/updateState")
    public boolean updateState(String sellerId){
        return sellersService.updateState(sellerId);
    }

    @Operation(summary="通过卖家Id查找卖家信息 返回值seller")
    @GetMapping("/sellersCtrl/select")
    @Parameter(name = "sellerId",description = "卖家Id")
    public Sellers select (String sellerId){
        return sellersService.select(sellerId);
    }

    @Operation(summary="通过(条件：name，state)查询卖家 传入参数Sellers, page ; 返回值PageInfo<Sellers>")
    @Parameter(name="name" ,description = "卖家名字")
    @GetMapping("/sellersCtrl/list")
    public PageInfo<Sellers> list(Sellers sellers, Page page){
        // 分页设置
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        ArrayList<Sellers> list = sellersService.list(sellers);
        return new PageInfo<Sellers>(list);
    }
    @Operation(summary = "商家 需要 查询的 订单信息")
    @Parameter(name = "sellerNeed",description = "信息")
    @GetMapping("/sellersCtrl/orderList")
    public PageInfo<SellerNeed> getOrderList(SellerNeed sellerNeed,Page page){
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        ArrayList<SellerNeed> list = sellersService.getOrderList(sellerNeed);
        return new  PageInfo<SellerNeed>(list);
    }

    @Operation(summary = "上传卖家头像")
    @Parameter(name="pic",description = "用户头像图片" ,content = @Content(mediaType = "multipart/form-data")  )
    @Parameter(name="sellerId",description = "用户编号")
    @PostMapping("/sellersCtrl/upload")
    public String upload(@RequestPart("pic") MultipartFile pic, @RequestParam("buyerId") String sellerId)  {
        String fileName = null;
        log.debug("--------------------------" + sellerId);
        try {
            if (!pic.isEmpty() && ImageIO.read(pic.getInputStream()) != null) {
                //获取文件名
                fileName = pic.getOriginalFilename();
                //通过UUID 设置新的文件名
                fileName = UUIdUtil.getUserId() + fileName.substring(fileName.lastIndexOf("."));
                //创建目标文件
                File file = new File(fileName);
                //将照片保存到目标文件
                pic.transferTo(file);

                //将图片名保存到数据库
                Sellers sellers = new Sellers();
                sellers.setSellerId(sellerId);
                sellers.setYhtx(fileName);
                log.debug("--------------------------" + sellers);
                sellersService.updateYhtx(sellers);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }
}
